package com.zz.man.controller;

import com.zz.man.domian.Man;
import com.zz.man.domian.Result;
import com.zz.man.repository.ManRepository;
import com.zz.man.service.ManService;
import com.zz.man.utils.ResultUtil;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.validation.BindingResult;
import org.springframework.web.bind.annotation.*;

import javax.validation.Valid;
import java.util.List;
import java.util.Optional;

@RestController
public class GirlController {

    @Autowired
    private ManRepository manRepository;

    @Autowired
    private ManService manService;

    private final static Logger log = LoggerFactory.getLogger(GirlController.class);

    /**
     * 查询数据库男生列表
     *
     * @return
     */
    @GetMapping(value = "/man")
    public List<Man> getManlist() {
        log.info("我我我我我我我");
        System.out.println("我我我我我我我");
        return manRepository.findAll();
    }

    /**
     * 添加一个男生
     *
     * @return
     */
    @PostMapping(value = "/man/add")
    public Result<Man> manAdd(@Valid Man man, BindingResult bindingResult) {
        if (bindingResult.hasErrors()) {
            return ResultUtil.error(-1,bindingResult.getFieldError().getDefaultMessage());
        }
        System.out.println("我我我我我我我");
        Optional<Man> mans = Optional.ofNullable(man);
        man.setAge(mans.map(u->u.getAge()).orElse(null));
        man.setName(mans.map(u->u.getName()).orElse(null));
        man.setAddress(mans.map(u->u.getAddress()).orElse(null));
        Man save = manRepository.save(man);
        return ResultUtil.success(save);
//        if (save != null) {
//            return 1;
//        } else {
//            return -1;
//        }
    }

    /**
     * 查询一个男生
     *
     * @param id
     * @return
     */
    @PostMapping(value = "/man/{id}")
    public Optional<Man> findManone(@PathVariable("id") Integer id) {
        return manRepository.findById(id);
    }

    /**
     * 更新一个男生
     *
     * @param id
     * @param name
     * @param age
     * @return
     */
    @PutMapping(value = "/man")
    public Man updateMan(@RequestParam("id") Integer id, @RequestParam("name") String name, @RequestParam("age") Integer age) {
        System.out.println("我我我我我我我");
        Man man = new Man();
        man.setName(name);
        man.setAge(age);
        man.setId(id);
        return manRepository.save(man);
    }

    /**
     * 删除一个男生
     *
     * @param id
     */
    @DeleteMapping(value = "/man/{id}")
    public void mandelete(@PathVariable("id") Integer id) {
        manRepository.deleteById(id);
    }

    /**
     * 通过年龄查询男生列表
     *
     * @param age
     * @return
     */
    @GetMapping(value = "/man/{age}")
    public List<Man> findManByAge(@PathVariable("age") Integer age) {
        return manRepository.findByAge(age);
    }

    /**
     * 插入两个男生
     */
    @GetMapping(value = "/man/two")
    public void insetTwoMan() {
        manService.insertTwo();
    }


    /**
     * 通过id查询一个男生
     * @param id
     * @throws Exception
     */
    @GetMapping(value = "/man/getAge/{id}")
    public void getAge(@PathVariable("id") Integer id)throws Exception{
        manService.getAge(id);
    }
}
